#define _CRTDBG_MAP_ALLOC

#include <stdlib.h>
#include <fstream>
#include <crtdbg.h>

#include "engine.h"
#include <string>
#include <iostream>
#include <boost/archive/text_iarchive.hpp>
int main()
{
	char getName[20];
	std::cout << "Type filename to load) ";
	std::cin.getline(getName,20);
	std::cout << "\n";
	std::string filename(getName);
	using namespace fortress;
	Engine *game;
	game = new Engine;
	std::string *title = new std::string("Test");
	game->createWindow(80, 25, title, false);
	game->setWindowTitle(title);
	if (filename.empty())
	{
		game->createWorld();
		try {game->createPlayer();}
		catch (std::string e)
		{
			std::cerr << e << std::endl;
		}
		
	} else
	{
		std::ifstream loadFile(filename.c_str());
		try {

			boost::archive::text_iarchive ia(loadFile);
			ia >> *game;
			game->loadPlayer();
		}
		catch (std::ifstream::failure error)
		{
			std::cout << "Error: " << error.what() << "\n";
			abort();
		}
		catch (std::string e)
		{
			std::cerr << e << std::endl;
		}
		catch (...)
		{
			std::cerr << "Dunno.";
			abort();
		}
		
	}

	game->redraw();
	while (!game->isGameOver())
	{
		while (!game->waitForInput())
		{
				game->updateWorld(0);
		}
//		fprintf(stdout, "Turn\n");
		if (!game->isGameOver())
			game->updateWorld(1);
	}
	game->endGame();
	delete title;
	delete game;
	return 0;
}